package com.lepu.app.fun.index.bean;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.app.config.UserConfig;
import com.app.utils.Const;
import com.app.utils.DBHelper;
import com.lepu.app.fun.my.bean.BeanMyCollect;

@SuppressWarnings("serial")
public class BeanIndexNew  extends BeanMyCollect implements Serializable{
	private static final String TABLE_NAME = "NewRead";
	
    public int video;  //视频
    public int good;   //精选
    public int drug;   //药
    public int food;   //饮食
    public int sport;  //运动
    public int heart;  //心理
    public int life;   //生活
    public String time;   //刷新时间

    // db 字段
	public int id;
	public String targetId;
    
    public static BeanIndexNew parseJsonData(String dataString) {
    	BeanIndexNew beanIndexNews = new BeanIndexNew();
        try {
            JSONObject jsonObject = new JSONObject(dataString);
            JSONObject obj1 = (JSONObject) jsonObject.get("Result");
            JSONObject detail = obj1.getJSONObject("DetailInfo");

			beanIndexNews.time = detail.optString("CurrentTime");
			beanIndexNews.video = detail.optInt("ArticleCount_24");
			beanIndexNews.good = detail.optInt("ArticleCount_25");
			beanIndexNews.drug = detail.optInt("ArticleCount_19");
			beanIndexNews.food = detail.optInt("ArticleCount_20");
			beanIndexNews.sport = detail.optInt("ArticleCount_21");
			beanIndexNews.heart = detail.optInt("ArticleCount_22");
			beanIndexNews.life = detail.optInt("ArticleCount_23");
				
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return beanIndexNews;
    }
    
    public static boolean checkVideo(BeanIndexNew beanIndexNews){
    	boolean flag = false;
    	if(beanIndexNews != null && beanIndexNews.video >0 ){
    		flag = true;
    	}
		return flag;
    }
    
    public static boolean checkBaike(BeanIndexNew beanIndexNews){
    	boolean flag = false;
    	if(beanIndexNews != null && (beanIndexNews.good >0 || beanIndexNews.drug >0 || beanIndexNews.food >0
    			|| beanIndexNews.sport >0 || beanIndexNews.heart >0 || beanIndexNews.life >0)){
    		flag = true;
    	}
		return flag;
    }

    public static void setBaikeConfigCount(Context context, BeanIndexNew beanIndexNew){
    	UserConfig.setConfig(context, Const.CONFIG_GOOD_COUNT, beanIndexNew.good);
		UserConfig.setConfig(context, Const.CONFIG_DRUG_COUNT, beanIndexNew.drug);
		UserConfig.setConfig(context, Const.CONFIG_FOOD_COUNT, beanIndexNew.food);
		UserConfig.setConfig(context, Const.CONFIG_SPORT_COUNT, beanIndexNew.sport);
		UserConfig.setConfig(context, Const.CONFIG_HEART_COUNT, beanIndexNew.heart);
		UserConfig.setConfig(context, Const.CONFIG_LIFE_COUNT, beanIndexNew.life);
    }
    
    public static void cleanBaikeConfigCount(Context context){
    	UserConfig.setConfig(context, Const.CONFIG_GOOD_COUNT, 0);
		UserConfig.setConfig(context, Const.CONFIG_DRUG_COUNT, 0);
		UserConfig.setConfig(context, Const.CONFIG_FOOD_COUNT, 0);
		UserConfig.setConfig(context, Const.CONFIG_SPORT_COUNT, 0);
		UserConfig.setConfig(context, Const.CONFIG_HEART_COUNT, 0);
		UserConfig.setConfig(context, Const.CONFIG_LIFE_COUNT, 0);
    }
    //添加
    public static boolean insertTargetId(String targetId) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
        if (dbHelp == null)
            return flag;

		if(isExist(targetId))
		{
			return flag;
		}
        ContentValues values = new ContentValues();
        values.put("targetId", targetId);
        flag = dbHelp.insert(TABLE_NAME, null, values);

        return flag;
    }
    
	//
	public static boolean isExist(String targetId) {
		ArrayList<BeanIndexNew> provinces = new ArrayList<BeanIndexNew>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_MAIN);
		if (dbHelp == null)
			return false;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {
					"id", "targetId" }, "targetId=?", new String[] { targetId },
					null, null, null);

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		if (provinces.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	
	/**
	 * 解析cursor转换为对象
	 */
	private static void cursorParse(ArrayList<BeanIndexNew> msgArray,Cursor cursor) {
		while (cursor.moveToNext()) {
			BeanIndexNew beanTarget = new BeanIndexNew();
			beanTarget.id = (cursor.getInt(cursor.getColumnIndex("id")));
			beanTarget.targetId = (cursor.getString(cursor.getColumnIndex("targetId")));
			msgArray.add(beanTarget);
		}
	}
	
	
}
